﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using TrackerRealm.Jazz.Common;

namespace TrackerRealm.Jazz.Client
{
    /// <summary>
    /// An abstract "jWorkflow" class that provides a non-unique string Name.
    /// <para>Instances of classes that inherit from 'jName' will searchable in
    /// 'ClientNexus.Cache.NameObjects'.</para>
    /// <para>Also see '<see cref="jNameUnique"/>'.</para>
    /// </summary>
    [UniqueName(false)]
    [Jazz]
    public class jName:jObject
    {
        #region Constructor
        /// <summary>
        /// Constructor <para>Using this constructor will require</para>
        /// <para>1) a static 'CreateEmpty' method in the inheriting class or</para>
        /// <para>2) changing the settins in 'NexusConfig'.</para>
        /// </summary>
        /// <param name="nexus"></param>
        /// <param name="name"></param>
        protected jName(ClientNexusBase nexus, string name)
        :base(nexus)
        {
            if (name == null)
                name = "";
            this.Name = name;
        }
        /// <summary>
        /// constructor
        /// <para>Using this constructor will require use of the 'ClientNexus.Bind(thisInstance)' method.</para>
        /// </summary>
        /// <param name="name"></param>
        protected jName(string name)
        {
            if (name == null)
                this.name = "";
            else
                this.name = name;
            // no need to add to NamedObjects cache since this instance is added when Bound.
        }
        #endregion
        #region Name
        [BaseField]
        private string name;
        /// <summary>
        /// Returns a non unique name for this instance of a '<see cref="jObject"/>' class.
        /// </summary>
        public override string Name
        {
            get { return this.name; }
            internal protected set
            {
                // if clientNexus is null this object will be added to the Cache when it is bound.
                if (this.IsReadOnly) throw new AccessViolationException();
                if (this.name != "" && this.name != null && this.ClientNexus != null)
                    this.ClientNexus.Cache.NamedObjects.TryRemove(this);
                if (value == null) 
                    this.name = "";
                else 
                    this.name = value;
                if(this.name != "" && this.ClientNexus != null && this.Class != null)
                    this.ClientNexus.Cache.NamedObjects.Add(this, isNameUnique: false);
            }
        }
        #endregion
    }
}
